package com.gzhryc.shared_device.oem.code.place.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.enums.AgentRelationFuncModelEnum;
import com.gzhryc.shared_device.oem.code.place.dao.ClerkFuncModelDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.ClerkFuncModel;
import com.gzhryc.shared_device.oem.code.place.dao.enums.ClerkFuncModelEnum;
import com.gzhryc.shared_device.oem.code.place.dao.models.ClerkFuncModelCount;

public class ClerkFuncModelService {

	static Logger log = Logger.getLogger(ClerkFuncModelService.class);

    public static ClerkFuncModelService self() {
        return MultiDBTools.getService(ClerkFuncModelService.class, true);
    }

    public static ClerkFuncModelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, ClerkFuncModelService.class, true);
    }

    ClerkFuncModelDao dao;

    public ClerkFuncModelService(String jdbcKey) {
        this.dao = new ClerkFuncModelDao(jdbcKey);
    }
    
    public List<ClerkFuncModel> findByUserKeys(Long userId,Long merchantUserId){
    	if(userId != null && merchantUserId != null) {
    		Conditions condition = new Conditions(ClerkFuncModel.class);
    		condition.and().is("userId", userId);
    		condition.and().is("merchantUserId", merchantUserId);
    		
    		try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<ClerkFuncModel> findJoinByUserKeys(Long userId,Long merchantUserId){
    	if(userId != null && merchantUserId != null) {
    		Conditions condition = new Conditions(ClerkFuncModel.class);
    		condition.and().is("userId", userId);
    		condition.and().is("merchantUserId", merchantUserId);
    		
    		try {
				return dao.findJoinByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public ClerkFuncModel getJoinByKeys(Long userId,Long merchantUserId,Long funcModelId) {
    	if(userId != null && merchantUserId != null && funcModelId != null) {
    		Conditions condition = new Conditions(ClerkFuncModel.class);
    		condition.and().is("userId", userId);
    		condition.and().is("merchantUserId", merchantUserId);
    		condition.and().is("funcModelId", funcModelId);
    		
    		try {
				return dao.getJoin(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<ClerkFuncModelCount> findFuncModelCountByUserKeys(List<Long> merchantUserIds,List<Long> userIds){
        if(merchantUserIds != null && merchantUserIds != null && userIds != null && userIds.size() > 0){
            Conditions conditions = new Conditions(ClerkFuncModel.class);
            conditions.and().in("merchantUserIds", merchantUserIds);
            conditions.and().in("userId",userIds);

            try {
                return dao.countGroupMerchantAndUserId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public boolean open(Long userId,Long merchantUserId,Long funcModelId) throws LogicException{
    	ClerkFuncModel clerkFuncModel = ClerkFuncModelService.self().getJoinByKeys(userId,merchantUserId,funcModelId);
    	if(clerkFuncModel != null) {
    		if(ClerkFuncModelEnum.State.Open.index().equals(clerkFuncModel.getState())) {
    			return true;
    		}else {
    			Conditions condition = new Conditions(ClerkFuncModel.class);
        		condition.and().is("userId", userId);
        		condition.and().is("merchantUserId", merchantUserId);
        		condition.and().is("funcModelId", funcModelId);
        		
        		ClerkFuncModel updateEntity = new ClerkFuncModel();
        		updateEntity.setState(ClerkFuncModelEnum.State.Open.index());
        		
        		try {
    				return dao.updateNotNull(updateEntity, condition) > 0;
    			} catch (SQLException e) {
    				log.error(e.getMessage(), e);
    			}
    		}
    	}else {
    		clerkFuncModel = new ClerkFuncModel();
    		clerkFuncModel.setUserId(userId);
            clerkFuncModel.setMerchantUserId(merchantUserId);
            clerkFuncModel.setFuncModelId(funcModelId);
            clerkFuncModel.setState(AgentRelationFuncModelEnum.State.Open.index());
            clerkFuncModel.setCreateDate(new Date());
            
            try {
				return dao.insert(clerkFuncModel) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return  false;
    }
    
    public boolean deleteByKeys(Long userId,Long merchantUserId,Long funcModelId) throws LogicException{
    	if(userId != null && merchantUserId != null && funcModelId != null) {
    		Conditions condition = new Conditions(ClerkFuncModel.class);
    		condition.and().is("userId", userId);
    		condition.and().is("merchantUserId", merchantUserId);
    		condition.and().is("funcModelId", funcModelId);
    		
    		try {
				return dao.deleteByCondition(condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
}
